package configuration

import (
	"administrator/app/mapper"
	"administrator/app/model"
	"gitee.com/aurora-engine/utils/rep"
	"gitee.com/aurora-engine/web"
	"github.com/pkg/errors"
)

type RoleManagement struct {
	*mapper.RoleManageMapper
}

func NewRoleManagement() web.Constructor {
	return func() any {
		return &RoleManagement{}
	}
}

func (manage *RoleManagement) GetOrgRole(args map[string]int) {

}

// GetRoleMenuAuth
// @Description :
// @Param args :
// [Get] => /api/configuration/role/menu/list
func (manage *RoleManagement) GetRoleMenuAuth(args map[string]int) (rep.Response, error) {
	var err error
	var list []*model.MenuRouter
	if list, err = manage.SelectMenuAuths(args); err != nil {
		return rep.Fail(err, "菜单权限查询失败"), err
	}
	return rep.Success(list), nil
}

// MenuAuthAdd
// @Description :
// @Param args :
// [Post] => /api/configuration/role/auth/menu/add
func (manage *RoleManagement) MenuAuthAdd(args map[string]any) (rep.Response, error) {
	var err error
	if _, ok := args["menus"]; !ok {
		err = errors.New("参数错误")
		return rep.Fail(err, "菜单权限错误"), err
	}
	if _, ok := args["roleId"]; !ok {
		err = errors.New("参数错误")
		return rep.Fail(err, "角色错误"), err
	}
	var roleId float64
	ok := false
	if roleId, ok = (args["roleId"]).(float64); !ok {
		err = errors.New("参数错误")
		return rep.Fail(err, "角色错误"), err
	}

	menus := args["menus"].([]any)
	auths := make([]*model.Auth, len(menus))
	for index, menu := range menus {
		auths[index] = &model.Auth{
			RoleId: int(roleId),
			AuthId: int(menu.(float64)),
		}
	}
	args["auths"] = auths
	if err = manage.InsertMenuAuth(args); err != nil {
		return rep.Fail(err, "授权新增失败"), err
	}
	return rep.Success(nil), nil
}

// MenuAuthDel
// @Description :
// @Param args :
// [Post] => /api/configuration/role/auth/menu/del
func (manage *RoleManagement) MenuAuthDel(args map[string]any) (rep.Response, error) {
	var err error
	if _, ok := args["menus"]; !ok {
		err = errors.New("参数错误")
		return rep.Fail(err, "菜单权限错误"), err
	}
	if _, ok := args["roleId"]; !ok {
		err = errors.New("参数错误")
		return rep.Fail(err, "角色错误"), err
	}
	var roleId float64
	ok := false
	if roleId, ok = (args["roleId"]).(float64); !ok {
		err = errors.New("参数错误")
		return rep.Fail(err, "角色错误"), err
	}
	menus := args["menus"].([]any)
	ids := make([]int, len(menus))
	for index, menu := range menus {
		ids[index] = int(menu.(float64))
	}
	args["roleId"] = roleId
	args["menus"] = ids

	// todo 删除授权时候需要判断 当前的菜单权限是否被分配到用户使用,需要先删除正在使用的用户授权,才能删除角色的权限
	var list []*model.RoleUserMenuAuth
	if list, err = manage.IsAuthUserMenu(args); err != nil {
		return rep.Fail(err, "授权删除失败"), err
	}
	if len(list) > 0 {
		err = errors.New("角色菜单权限在用中")
		return rep.Fail(err, "授权删除失败"), err
	}
	if err = manage.DeleteMenuAuth(args); err != nil {
		return rep.Fail(err, "授权删除失败"), err
	}
	return rep.Success(nil), nil
}

// GetFuncAuth
// @Description :
// @Param args :
// [Get] => /api/configuration/role/func/list
func (manage *RoleManagement) GetFuncAuth(args map[string]any) (rep.Response, error) {
	var err error
	var list []*model.FuncRouter
	if list, err = manage.SelectFuncAuths(args); err != nil {
		return rep.Fail(err, "查询失败"), err
	}
	return rep.Success(list), nil
}

// FuncAuthAdd
// @Description :
// @Param args :
// [Post] => /api/configuration/role/auth/func/add
func (manage *RoleManagement) FuncAuthAdd(args map[string]any) (rep.Response, error) {
	var err error
	if _, ok := args["funcs"]; !ok {
		err = errors.New("参数错误")
		return rep.Fail(err, "功能权限错误"), err
	}
	if _, ok := args["roleId"]; !ok {
		err = errors.New("参数错误")
		return rep.Fail(err, "角色错误"), err
	}
	var roleId float64
	ok := false
	if roleId, ok = (args["roleId"]).(float64); !ok {
		err = errors.New("参数错误")
		return rep.Fail(err, "角色错误"), err
	}

	funcs := args["funcs"].([]any)
	auths := make([]*model.Auth, len(funcs))
	for index, fun := range funcs {
		auths[index] = &model.Auth{
			RoleId: int(roleId),
			AuthId: int(fun.(float64)),
		}
	}
	args["auths"] = auths
	if err = manage.InsertFuncAuth(args); err != nil {
		return rep.Fail(err, "授权新增失败"), err
	}
	return rep.Success(nil), nil
}

// FuncAuthDel
// @Description :
// @Param args :
// [Post] => /api/configuration/role/auth/func/del
func (manage *RoleManagement) FuncAuthDel(args map[string]any) (rep.Response, error) {
	var err error
	if _, ok := args["funcs"]; !ok {
		err = errors.New("参数错误")
		return rep.Fail(err, "功能权限错误"), err
	}
	if _, ok := args["roleId"]; !ok {
		err = errors.New("参数错误")
		return rep.Fail(err, "角色错误"), err
	}
	var roleId float64
	ok := false
	if roleId, ok = (args["roleId"]).(float64); !ok {
		err = errors.New("参数错误")
		return rep.Fail(err, "角色错误"), err
	}
	funcs := args["funcs"].([]any)
	ids := make([]int, len(funcs))
	for index, fun := range funcs {
		ids[index] = int(fun.(float64))
	}
	args["roleId"] = roleId
	args["funcs"] = ids

	// todo 删除授权时候需要判断 当前的菜单权限是否被分配到用户使用,需要先删除正在使用的用户授权,才能删除角色的权限
	var list []*model.RoleUserFuncAuth
	if list, err = manage.IsAuthUserFunc(args); err != nil {
		return rep.Fail(err, "授权删除失败"), err
	}
	if len(list) > 0 {
		err = errors.New("角色功能权限在用中")
		return rep.Fail(err, "授权删除失败"), err
	}
	if err = manage.DeleteFuncAuth(args); err != nil {
		return rep.Fail(err, "授权删除失败"), err
	}
	return rep.Success(nil), nil
}
